home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ue312src.zip / WMCS.C < prev    next >
C/C++ Source or Header  |  1993-03-02  |  10KB  |  376 lines

  1. /*    WMCS.C:    Operating specific I/O and Spawning functions
  2.         for the WICAT computer/operating system
  3.         for MicroEMACS 3.12
  4.         (C)Copyright 1993 by Daniel M. Lawrence
  5. */
  6.  
  7. #include        <stdio.h>
  8. #include    "estruct.h"
  9. #include    "eproto.h"
  10. #if    WMCS
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #include    "/sysincl.sys/devtdisp.h"
  15. #include    "/sysincl.sys/dstatdisp.h"
  16.  
  17. devicetable    dtable;
  18. devicestatus    dstat;
  19. ushort    xmdstat = 0x019d;
  20. ushort    xmdsave;
  21. ushort    xmdmask = 0xfe62;
  22.  
  23. /*
  24.  * This function is called once to set up the terminal device streams.
  25.  * On VMS, it translates TT until it finds the terminal, then assigns
  26.  * a channel to it and sets it raw. On CPM it is a no-op.
  27.  */
  28. ttopen()
  29. {
  30.     strcpy(os, "WMCS");
  31.  
  32.     /* set device status */
  33.     _giodst(1,&dtable,sizeof(dtable),&dstat);
  34.     xmdsave = dstat.class.tty.dstyflags1;
  35.     dstat.class.tty.dstyflags1 &= xmdmask;    /* clear the bits we need to use */
  36.     dstat.class.tty.dstyflags1 |= xmdstat;    /* set our status bits */
  37.     _siodst(1,&dstat);    /* set device status */
  38.  
  39.     /* on all screens we are not sure of the initial position
  40.        of the cursor                    */
  41.     ttrow = 999;
  42.     ttcol = 999;
  43. }
  44.  
  45. /*
  46.  * This function gets called just before we go back home to the command
  47.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  48.  * Another no-operation on CPM.
  49.  */
  50. ttclose()
  51. {
  52.     /* reset device status back to saved values */
  53.     dstat.class.tty.dstyflags1 = xmdsave;
  54.     _siodst(1,&dstat);
  55. }
  56.  
  57. /*
  58.  * Write a character to the display. On VMS, terminal output is buffered, and
  59.  * we just put the characters in the big array, after checking for overflow.
  60.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  61.  * MS-DOS (use the very very raw console output routine).
  62.  */
  63. ttputc(c)
  64. {
  65.         fputc(c, stdout);
  66. }
  67.  
  68. /*
  69.  * Flush terminal buffer. Does real work where the terminal output is buffered
  70.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  71.  */
  72. ttflush()
  73. {
  74.         fflush(stdout);
  75. }
  76.  
  77. /*
  78.  * Read a character from the terminal, performing no editing and doing no echo
  79.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  80.  * simple on CPM, because the system can do exactly what you want.
  81.  */
  82. ttgetc()
  83. {
  84.     char c;
  85.     long trns;
  86.     _read(1,-1,0,-1,&c,1,&trns);
  87.     return((int)(c & 0x7f));
  88. }
  89.  
  90. #if    TERMCAP
  91. /* get a character with timeout */
  92. mttgetc()
  93. {
  94.     char c;
  95.     long trns,status;
  96.     
  97.     status = _read(1,-1,0,4,&c,1,&trns);
  98.     if(status) return(-1);
  99.     return((int)(c & 0x7f));
  100. }
  101. #endif
  102.  
  103. #if    TYPEAH
  104. /* typahead:    Check to see if any characters are already in the
  105.         keyboard buffer
  106. */
  107.  
  108. typahead()
  109.  
  110. {
  111. /*  This doesnt seem to work even though it should
  112.     So I leave it commented out...
  113.  
  114.     devicetable    xdtable;
  115.     devicestatus    xdstat;
  116.     
  117.     _giodst(1,&xdtable,sizeof(xdtable),&xdstat);
  118.     return(dstat.class.tty.dstyinputcnt);
  119. */
  120.     return(FALSE);
  121. }
  122. #endif
  123.  
  124. /*
  125.  * Create a subjob with a copy of the command intrepreter in it. When the
  126.  * command interpreter exits, mark the screen as garbage so that you do a full
  127.  * repaint. Bound to "^X C". The message at the start in VMS puts out a newline.
  128.  * Under some (unknown) condition, you don't get one free when DCL starts up.
  129.  */
  130. spawncli(f, n)
  131. {
  132.         register char *cp;
  133.         char *getenv();
  134.  
  135.     /* don't allow this command if restricted */
  136.     if (restflag)
  137.         return(resterr());
  138.  
  139.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  140.         TTflush();
  141.         TTclose();                              /* stty to old settings */
  142.     if ((cp = getenv("SYS$CIP")) != NULL && *cp != '\0')
  143.                 system(cp);
  144.         else
  145.         system("CIP");
  146.  
  147.         sgarbf = TRUE;
  148.     sleep(2);
  149.         TTopen();
  150.         return(TRUE);
  151. }
  152.  
  153. /*
  154.  * Run a one-liner in a subjob. When the command returns, wait for a single
  155.  * character to be typed, then mark the screen as garbage so a full repaint is
  156.  * done. Bound to "C-X !".
  157.  */
  158. spawn(f, n)
  159. {
  160.         register int    s;
  161.         char            line[NLINE];
  162.  
  163.     /* don't allow this command if restricted */
  164.     if (restflag)
  165.         return(resterr());
  166.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  167.                 return (s);
  168.         TTputc('\n');                /* Already have '\r'    */
  169.         TTflush();
  170.         TTclose();                              /* stty to old modes    */
  171.         system(line);
  172.         TTopen();
  173.         mlputs(TEXT188);                        /* Pause.               */
  174. /*             "[End]" */
  175.         TTflush();
  176.         while ((s = tgetc()) != '\r' && s != ' ')
  177.                 ;
  178.         sgarbf = TRUE;
  179.         return (TRUE);
  180. }
  181.  
  182. /*
  183.  * Run an external program with arguments. When it returns, wait for a single
  184.  * character to be typed, then mark the screen as garbage so a full repaint is
  185.  * done. Bound to "C-X $".
  186.  */
  187.  
  188. execprg(f, n)
  189.  
  190. {
  191.         register int    s;
  192.         char            line[NLINE];
  193.  
  194.     /* don't allow this command if restricted */
  195.     if (restflag)
  196.         return(resterr());
  197.  
  198.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  199.                 return (s);
  200.         TTputc('\n');                /* Already have '\r'    */
  201.         TTflush();
  202.         TTclose();                              /* stty to old modes    */
  203.         system(line);
  204.         TTopen();
  205.         mlputs(TEXT188);                        /* Pause.               */
  206. /*             "[End]" */
  207.         TTflush();
  208.         while ((s = tgetc()) != '\r' && s != ' ')
  209.                 ;
  210.         sgarbf = TRUE;
  211.         return (TRUE);
  212. }
  213.  
  214. /*
  215.  * Pipe a one line command into a window
  216.  * Bound to ^X @
  217.  */
  218. pipecmd(f, n)
  219. {
  220.         register int    s;    /* return status from CLI */
  221.     register WINDOW *wp;    /* pointer to new window */
  222.     register BUFFER *bp;    /* pointer to buffer to zot */
  223.         char    line[NLINE];    /* command line send to shell */
  224.     static char bname[] = "command";
  225.  
  226.     static char filnam[NSTRING] = "command";
  227.  
  228.     /* don't allow this command if restricted */
  229.     if (restflag)
  230.         return(resterr());
  231.  
  232.     /* get the command to pipe in */
  233.         if ((s=mlreply("@", line, NLINE)) != TRUE)
  234.                 return(s);
  235.  
  236.     /* get rid of the command output buffer if it exists */
  237.         if ((bp=bfind(bname, FALSE, 0)) != FALSE) {
  238.         /* try to make sure we are off screen */
  239.         wp = wheadp;
  240.         while (wp != NULL) {
  241.             if (wp->w_bufp == bp) {
  242.                 onlywind(FALSE, 1);
  243.                 break;
  244.             }
  245.             wp = wp->w_wndp;
  246.         }
  247.         if (zotbuf(bp) != TRUE)
  248.  
  249.             return(FALSE);
  250.     }
  251.  
  252.         TTputc('\n');                /* Already have '\r'    */
  253.         TTflush();
  254.         TTclose();                              /* stty to old modes    */
  255.     strcat(line,">");
  256.     strcat(line,filnam);
  257.         system(line);
  258.         TTopen();
  259.         TTflush();
  260.         sgarbf = TRUE;
  261.         s = TRUE;
  262.  
  263.     if (s != TRUE)
  264.         return(s);
  265.  
  266.     /* split the current window to make room for the command output */
  267.     if (splitwind(FALSE, 1) == FALSE)
  268.             return(FALSE);
  269.  
  270.     /* and read the stuff in */
  271.     if (getfile(filnam, FALSE) == FALSE)
  272.         return(FALSE);
  273.  
  274.     /* make this window in VIEW mode, update all mode lines */
  275.     curwp->w_bufp->b_mode |= MDVIEW;
  276.     wp = wheadp;
  277.     while (wp != NULL) {
  278.         wp->w_flag |= WFMODE;
  279.         wp = wp->w_wndp;
  280.     }
  281.  
  282.     /* and get rid of the temporary file */
  283.     unlink(filnam);
  284.     return(TRUE);
  285. }
  286.  
  287. /*
  288.  * filter a buffer through an external DOS program
  289.  * Bound to ^X #
  290.  */
  291. filter(f, n)
  292.  
  293. {
  294.         register int    s;    /* return status from CLI */
  295.     register BUFFER *bp;    /* pointer to buffer to zot */
  296.         char line[NLINE];    /* command line send to shell */
  297.     char tmpnam[NFILEN];    /* place to store real file name */
  298.     static char bname1[] = "fltinp";
  299.  
  300.     static char filnam1[] = "fltinp";
  301.     static char filnam2[] = "fltout";
  302.  
  303.     /* don't allow this command if restricted */
  304.     if (restflag)
  305.         return(resterr());
  306.  
  307.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  308.         return(rdonly());    /* we are in read only mode    */
  309.  
  310.     /* get the filter name and its args */
  311.         if ((s=mlreply("#", line, NLINE)) != TRUE)
  312.                 return(s);
  313.  
  314.     /* setup the proper file names */
  315.     bp = curbp;
  316.     strcpy(tmpnam, bp->b_fname);    /* save the original name */
  317.     strcpy(bp->b_fname, bname1);    /* set it to our new one */
  318.  
  319.     /* write it out, checking for errors */
  320.     if (writeout(filnam1, "w") != TRUE) {
  321.         mlwrite(TEXT2);
  322. /*                      "[Cannot write filter file]" */
  323.         strcpy(bp->b_fname, tmpnam);
  324.         return(FALSE);
  325.     }
  326.  
  327.         TTputc('\n');                /* Already have '\r'    */
  328.         TTflush();
  329.         TTclose();                              /* stty to old modes    */
  330.     strcat(line," <fltinp >fltout");
  331.         system(line);
  332.         TTopen();
  333.         TTflush();
  334.         sgarbf = TRUE;
  335.         s = TRUE;
  336.  
  337.     /* on failure, escape gracefully */
  338.     if (s != TRUE || (readin(filnam2,FALSE) == FALSE)) {
  339.         mlwrite(TEXT3);
  340. /*                      "[Execution failed]" */
  341.         strcpy(bp->b_fname, tmpnam);
  342.         unlink(filnam1);
  343.         unlink(filnam2);
  344.         return(s);
  345.     }
  346.  
  347.     /* reset file name */
  348.     strcpy(bp->b_fname, tmpnam);    /* restore name */
  349.     bp->b_flag |= BFCHG;        /* flag it as changed */
  350.  
  351.     /* and get rid of the temporary file */
  352.     unlink(filnam1);
  353.     unlink(filnam2);
  354.     return(TRUE);
  355. }
  356.  
  357. /* return a system dependant string with the current time */
  358.  
  359. char *PASCAL NEAR timeset()
  360.  
  361. {
  362.     register char *sp;    /* temp string pointer */
  363.     char buf[16];        /* time data buffer */
  364.     extern char *ctime();
  365.  
  366.     time(buf);
  367.     sp = ctime(buf);
  368.     sp[strlen(sp)-1] = 0;
  369.     return(sp);
  370. }
  371. #else
  372. wmcshello()
  373. {
  374. }
  375. #endif
  376.